/**
 * 文件名    ： DownloadPicFromURL.java
 * 版权       : XX科技有限公司。
 * 描述       : <描述>
 * 修改时间：2019年8月28日
 * 修改内容：<修改内容>
 */
package com.example.demo.application;
import java.io.ByteArrayOutputStream;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.net.URL;


 

public class DownloadPicFromURL {

    public static void main(String[] args) {

        String url = "http://229032.uniflows.com/design/share/2018/09/29/157691/229032/";
        String str = "001";
        String f = ".jpg?r=230233230234";
        String at = ".jpg";
        
        String path="e:/book/";
        int i =5;
        
        try {
			for(i =1;i<=456;i++) {
				str = add0(String.valueOf(i));
				downloadPicture(url+str+f,path+str+at);
				Thread.sleep(900);
				if(i%8 ==0) {
					Thread.sleep(930);
				}
				if(i%40 ==0) {
					System.out.println(add0("-------------------------"+String.valueOf(i))+"-------------------------");
				}
			}
		
        } catch (Exception e) {
        	System.out.println(add0("-------------------------"+String.valueOf(i))+"-------------------------");
        	e.printStackTrace();
        }
        

    }
    public static String add0(String num) {
    	StringBuilder r = new StringBuilder();
    	for(int i = num.length();i<3;i++) {
    		r.append("0");
    	}
    	r.append(num);
		return r.toString();
    
    }
    

    //链接url下载图片

    private static void downloadPicture(String urlList,String path) throws IOException {

        URL url = null;

            url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();

    }
}